Added documentation about how to use the unit test runner.
[adiumx.git] / Plugins / Purple Service / ESPurpleNotifyEmailController.m
blob859f4db20f9c485f1c1d638b7aacdc62cd4d7ad4
1 /* 
2  * Adium is the legal property of its developers, whose names are listed in the copyright file included
3  * with this source distribution.
4  * 
5  * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
6  * General Public License as published by the Free Software Foundation; either version 2 of the License,
7  * or (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
10  * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
11  * Public License for more details.
12  * 
13  * You should have received a copy of the GNU General Public License along with this program; if not,
14  * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
15  */
17 #import "ESPurpleNotifyEmailController.h"
18 #import <AdiumLibpurple/SLPurpleCocoaAdapter.h>
19 #import <AdiumLibpurple/PurpleCommon.h>
20 #import <Adium/ESTextAndButtonsWindowController.h>
21 #import <Adium/AIContactAlertsControllerProtocol.h>
22 #import <Adium/AIAccount.h>
23 #import <AIUtilities/AIObjectAdditions.h>
25 @interface ESPurpleNotifyEmailController (PRIVATE)
26 + (void)openURLString:(NSString *)urlString;
27 @end
29 @implementation ESPurpleNotifyEmailController
31 /*!
32  * @brief Handle the notification of emails
33  *
34  * This may be called from the purple thread.
35  */
36 + (void *)handleNotifyEmailsForAccount:(AIAccount *)account count:(size_t)count detailed:(BOOL)detailed subjects:(const char **)subjects froms:(const char **)froms tos:(const char **)tos urls:(const char **)urls
38         NSFontManager                           *fontManager = [NSFontManager sharedFontManager];
39         NSFont                                          *messageFont = [NSFont messageFontOfSize:11];
40         NSMutableParagraphStyle         *centeredParagraphStyle;
41         NSMutableAttributedString   *message;
42         
43         centeredParagraphStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
44         [centeredParagraphStyle setAlignment:NSCenterTextAlignment];
45         message = [[NSMutableAttributedString alloc] init];
46         
47         //Title
48         NSString                *title;
49         NSFont                  *titleFont;
50         NSDictionary    *titleAttributes;
51         
52         title = AILocalizedString(@"You have mail!\n",nil);
53         titleFont = [fontManager convertFont:[NSFont messageFontOfSize:12]
54                                                          toHaveTrait:NSBoldFontMask];
55         titleAttributes = [NSDictionary dictionaryWithObjectsAndKeys:titleFont,NSFontAttributeName,
56                 centeredParagraphStyle,NSParagraphStyleAttributeName,nil];
57         
58         [message appendAttributedString:[[[NSAttributedString alloc] initWithString:title
59                                                                                                                                          attributes:titleAttributes] autorelease]];
60         
61         //Message
62         NSString                *numberMessage;
63         NSDictionary    *numberMessageAttributes;
64         NSString                *yourName;
65         
66         if (account) {
67                 yourName = [account formattedUID];
68         } else if (tos && *tos) {
69                 yourName = [NSString stringWithUTF8String:*tos];
70         } else {
71                 yourName = nil;
72         }
74         if (yourName && [yourName length]) {
75                 numberMessage = ((count == 1) ? 
76                                                  [NSString stringWithFormat:AILocalizedString(@"%@ has 1 new message.",nil), yourName] :
77                                                  [NSString stringWithFormat:AILocalizedString(@"%@ has %u new messages.",nil), yourName, count]);
79         } else {
80                 numberMessage = ((count == 1) ? 
81                                                  AILocalizedString(@"You have 1 new message.",nil) :
82                                                  [NSString stringWithFormat:AILocalizedString(@"You have %u new messages.",nil), count]);               
83         }
85         numberMessageAttributes = [NSDictionary dictionaryWithObjectsAndKeys:messageFont,NSFontAttributeName,
86                 centeredParagraphStyle,NSParagraphStyleAttributeName,nil];
87         
88         [message appendAttributedString:[[[NSAttributedString alloc] initWithString:numberMessage
89                                                                                                                                          attributes:numberMessageAttributes] autorelease]];
90         
91         if (count == 1) {
92                 BOOL    haveFroms    = (froms    != NULL);
93                 BOOL    haveSubjects = (subjects != NULL);
94                 
95                 if (haveFroms || haveSubjects) {
96                         NSFont                  *fieldFont;
97                         NSDictionary    *fieldAttributed, *infoAttributed;
98                         
99                         fieldFont =  [fontManager convertFont:messageFont
100                                                                           toHaveTrait:NSBoldFontMask];
101                         fieldAttributed = [NSDictionary dictionaryWithObject:fieldFont forKey:NSFontAttributeName];
102                         infoAttributed = [NSDictionary dictionaryWithObject:messageFont forKey:NSFontAttributeName];
103                         
104                         //Skip a line
105                         [[message mutableString] appendString:@"\n\n"];
106                         
107                         if (haveFroms) {
108                                 NSString        *fromString = [NSString stringWithUTF8String:(*froms)];
109                                 if (fromString && [fromString length]) {
110                                         [message appendAttributedString:[[[NSAttributedString alloc] initWithString:AILocalizedString(@"From: ",nil)
111                                                                                                                                                                          attributes:fieldAttributed] autorelease]];
112                                         [message appendAttributedString:[[[NSAttributedString alloc] initWithString:fromString
113                                                                                                                                                                          attributes:infoAttributed] autorelease]];
114                                 }
115                         }
116                         
117                         if (haveFroms && haveSubjects) {
118                                 [[message mutableString] appendString:@"\n"];
119                         }
120                         
121                         if (haveSubjects) {
122                                 NSString        *subjectString = [NSString stringWithUTF8String:(*subjects)];
123                                 if (subjectString && [subjectString length]) {
124                                         [message appendAttributedString:[[[NSAttributedString alloc] initWithString:AILocalizedString(@"Subject: ",nil)
125                                                                                                                                                                          attributes:fieldAttributed] autorelease]];
126                                         AILog(@"%@: %@ appending %@",self,message,subjectString);
127                                         [message appendAttributedString:[[[NSAttributedString alloc] initWithString:subjectString
128                                                                                                                                                                          attributes:infoAttributed] autorelease]];                              
129                                 } else {
130                                         AILog(@"Got an invalid subjectString from %s",*subjects);
131                                 }
132                         }
133                 }
134         }
135         
136         NSMutableDictionary *infoDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:title,@"Title",
137                 message,@"Message",nil];
138         
139         NSString        *urlString = (urls ? [NSString stringWithUTF8String:urls[0]] : nil);
141         if (urlString) {
142                 [infoDict setObject:urlString forKey:@"URL"];
143         }
144         
145         [self mainPerformSelector:@selector(showNotifyEmailWindowForAccount:withMessage:URLString:)
146                                    withObject:account
147                                    withObject:message
148                                    withObject:(urlString ? urlString : nil)];
150         [centeredParagraphStyle release];
151         [message release];
152         
153         return NULL;
157  * @brief Show the New Mail message
159  * Displays the New Mail message, optionally offerring an Open Mail button (if a URL to open the webmail is passed).
161  * @param account The account which received new mail
162  * @param inMessage An attributed message describing the new mail
163  * @param inURLString The URL to the appropriate webmail, or nil if no webmail link is available
164  */
165 + (void)showNotifyEmailWindowForAccount:(AIAccount *)account withMessage:(NSAttributedString *)inMessage URLString:(NSString *)inURLString
166 {       
167         [ESTextAndButtonsWindowController showTextAndButtonsWindowWithTitle:AILocalizedString(@"New Mail",nil)
168                                                                                                                   defaultButton:nil
169                                                                                                                 alternateButton:(inURLString ? 
170                                                                                                                                                  AILocalizedString(@"Open Mail",nil) :
171                                                                                                                                                  nil)
172                                                                                                                         otherButton:nil
173                                                                                                                            onWindow:nil
174                                                                                                           withMessageHeader:nil
175                                                                                                                          andMessage:inMessage
176                                                                                                                                  target:self
177                                                                                                                            userInfo:inURLString];       
178         
179         //XXX - Hook this to the account for listobject
180         [[[AIObject sharedAdiumInstance] contactAlertsController] generateEvent:ACCOUNT_RECEIVED_EMAIL
181                                                                                                                           forListObject:account
182                                                                                                                                    userInfo:[inMessage string]
183                                                                                            previouslyPerformedActionIDs:nil];   
187  * @brief Window was closed, either by a button being clicked or the user closing it
188  */
189 + (BOOL)textAndButtonsWindowDidEnd:(NSWindow *)window returnCode:(AITextAndButtonsReturnCode)returnCode userInfo:(id)userInfo
191         switch (returnCode) {
192                 case AITextAndButtonsAlternateReturn:
193                         if (userInfo) [self openURLString:userInfo];
194                         break;
196                 case AITextAndButtonsDefaultReturn:
197                 case AITextAndButtonsOtherReturn:
198                 case AITextAndButtonsClosedWithoutResponse:
199                         //No action needed
200                         break;
201         }
202         
203         return YES;
207  * @brief Open a URL string from the open mail window
209  * The urlString could either be a web address or a path to a local HTML file we are supposed to load.
210  * The local HTML file will be in the user's temp directory, which Purple obtains with g_get_tmp_dir()...  
211  * so we will, too.
212  */ 
213 + (void)openURLString:(NSString *)urlString
215         if ([urlString rangeOfString:[NSString stringWithUTF8String:g_get_tmp_dir()]].location != NSNotFound) {
216                 //Local HTML file
217                 CFURLRef        appURL = NULL;
218                 OSStatus        err;
219                 
220                 /* Obtain the default http:// handler. We don't care what would handle _this file_ (its extension doesn't matter)
221                  * nor what normally happens when the user opens a .html file since that is, on many systems, an HTML editor.
222                  * Instead, we want to know what application to use for viewing web pages... and then open this file in it.
223                  */
224                 err = LSGetApplicationForURL((CFURLRef)[NSURL URLWithString:@"http://www.adiumx.com"],
225                                                                          kLSRolesViewer,
226                                                                          /*outAppRef*/ NULL,
227                                                                          &appURL);
228                 if (err == noErr) {
229                         [[NSWorkspace sharedWorkspace] openFile:[urlString stringByExpandingTildeInPath]
230                                                                         withApplication:[(NSURL *)appURL path]];
231                 } else {
232                         NSURL           *url;
233                         
234                         //Web address
235                         url = [NSURL URLWithString:urlString];
236                         [[NSWorkspace sharedWorkspace] openURL:url];
237                 }
238                 
239                 if (appURL) {
240                         //LSGetApplicationForURL() requires us to release the appURL when we are done with it
241                         CFRelease(appURL);
242                 }
243                 
244         } else {
245                 NSURL           *emailURL;
246                 
247                 //Web address
248                 emailURL = [NSURL URLWithString:urlString];
249                 [[NSWorkspace sharedWorkspace] openURL:emailURL];
250         }
253 @end